
 
package com.junitUtil.spring;	
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;



/**
 * TODO(描述这个类的作用) 
 * 
 **/
public class DataBaseBean {
    public final static String[] default_config_properties = { "config.properties", "server.properties" };

  public  String configPropertiesPath ="";

  public  String dataBaseConfigPath ="" ;

  public  String jdbcDriver ="" ;

  public  String jdbcUrl ="" ;

  public  String jdbcUsername ="" ;

  public String jdbcPassword ="" ;

  public String jdbcMaxActive ="" ;

  public  String jdbcMaxIdle ="" ;

  public  String jdbcMinIdle ="" ;

  public void readProperty(){
      List<String> list=new ArrayList<>();
      list.addAll( Arrays.asList(default_config_properties));
      if(StringUtils.isNotEmpty(configPropertiesPath)){
          list.add(0,configPropertiesPath);
      } 
      for(String path:list){
          Properties properties = new Properties();
          try {
              Resource resource=  (new DefaultResourceLoader()).getResource(path);
            properties.load(resource.getInputStream());
        } catch (IOException e) {
            continue;
        }
          
          boolean hasRead=false;
          for(Object keyObj:properties.keySet()){
              String key=ObjectUtils.toString(keyObj,"");
              String value=properties.getProperty(key);
              if (StringUtils.containsIgnoreCase(key, "jdbc")||StringUtils.containsIgnoreCase(key, "database") ) {
                  if (StringUtils.containsIgnoreCase(key, "url")  ) {
                      this.jdbcUrl=value;
                  } else if (StringUtils.containsIgnoreCase(key, "driver") ) {
                      this.jdbcDriver=value;
                  }else if (StringUtils.containsIgnoreCase(key, "user")  ) {
                      this.jdbcUsername=value;
                  } else if (StringUtils.containsIgnoreCase(key, "password")  ) {
                      this.jdbcPassword=value;
                  }else if (StringUtils.containsIgnoreCase(key, "maxIdle") ) {
                      this.jdbcMaxIdle=value;
                  }else if (StringUtils.containsIgnoreCase(key, "maxActive")  ) {
                      this.jdbcMaxActive=value;
                  }else if (StringUtils.containsIgnoreCase(key, "minIdle")   ) {
                      this.jdbcMinIdle=value;
                  }
                  hasRead=true;
                   
              }
          }
          if(hasRead){
              configPropertiesPath=path;
              break;
          }
      }
  }   
    
    
    
    
    
}

